import { Router } from 'express';
import { body, query } from 'express-validator';
import { ClientVoteController } from '../../api/controllers/voteController';
import { authenticateClient } from '../../middleware/auth';
import { validateRequest } from '../../middleware/validation';

const router = Router();
const voteController = new ClientVoteController();

/**
 * @swagger
 * tags:
 *   name: 客户端投票
 *   description: 客户端投票相关接口
 */

/**
 * @swagger
 * /api/client/votes/current-activity:
 *   get:
 *     summary: 获取当前活动
 *     tags: [客户端投票]
 *     responses:
 *       200:
 *         description: 获取成功
 *       404:
 *         description: 当前没有进行中的活动
 */
router.get('/current-activity', voteController.getCurrentActivity);

/**
 * @swagger
 * /api/client/votes/activity/{activityId}/leaderboard:
 *   get:
 *     summary: 获取活动投票排行榜
 *     tags: [客户端投票]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: activityId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *         description: 返回数量限制
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动不存在或已结束
 */
router.get('/activity/:activityId/leaderboard', [
  authenticateClient,
  query('limit').optional().isInt({ min: 1, max: 100 }).withMessage('数量限制必须在1-100之间'),
  validateRequest
], voteController.getActivityVoteLeaderboard);

/**
 * @swagger
 * /api/client/votes/leaderboard:
 *   get:
 *     summary: 获取投票排行榜（兼容旧版本）
 *     tags: [客户端投票]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *         description: 返回数量限制
 *     responses:
 *       200:
 *         description: 获取成功
 *       401:
 *         description: 未授权
 */
router.get('/leaderboard', authenticateClient, voteController.getVoteLeaderboard);

/**
 * @swagger
 * /api/client/votes:
 *   post:
 *     summary: 投票
 *     tags: [客户端投票]
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - artistId
 *               - activityId
 *             properties:
 *               artistId:
 *                 type: integer
 *                 description: 艺人ID
 *               activityId:
 *                 type: integer
 *                 description: 活动ID
 *     responses:
 *       200:
 *         description: 投票成功
 *       400:
 *         description: 请求参数错误
 *       401:
 *         description: 未授权
 *       404:
 *         description: 活动或艺人不存在
 */
router.post('/', [
  authenticateClient,
  body('artistId')
    .isInt()
    .withMessage('艺人ID必须是整数'),
  body('activityId')
    .isInt()
    .withMessage('活动ID必须是整数'),
  validateRequest
], voteController.vote);

/**
 * @swagger
 * /api/client/votes/status:
 *   get:
 *     summary: 检查用户投票状态
 *     tags: [客户端投票]
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: activityId
 *         required: true
 *         schema:
 *           type: integer
 *         description: 活动ID
 *     responses:
 *       200:
 *         description: 获取成功
 *       400:
 *         description: 活动ID不能为空
 *       401:
 *         description: 未授权
 */
router.get('/status', [
  authenticateClient,
  query('activityId')
    .isInt()
    .withMessage('活动ID必须是整数'),
  validateRequest
], voteController.getVoteStatus);

export default router;
